package com.dashunzi.yueyutianqi.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dashunzi.yueyutianqi.R;
import com.dashunzi.yueyutianqi.base.BasePager;
import com.dashunzi.yueyutianqi.fragment.ContentFragment;
import com.dashunzi.yueyutianqi.pager.busPager;
import com.dashunzi.yueyutianqi.pager.smilePager;
import com.dashunzi.yueyutianqi.pager.trainPager;
import com.dashunzi.yueyutianqi.pager.weatherPager;
import com.dashunzi.yueyutianqi.view.NoScrollViewPager;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 主界面
 */
public class HomeActivity extends Activity {

    private LinearLayout flContent;

    private NoScrollViewPager vpContent;
    private RadioGroup rgGroup;
    private RadioButton rbWeather;
    private RadioButton rbTrain;
    private RadioButton rbBus;
    private RadioButton rbSmile;
    /**
     * 从选择城市界面传递过来的天气代号
     */
    private String mCountyCode;

    private void assignViews() {
        flContent = (LinearLayout) findViewById(R.id.fl_content);

        vpContent = (NoScrollViewPager) findViewById(R.id.vp_content);
        rgGroup = (RadioGroup) findViewById(R.id.rg_group);
        rbWeather = (RadioButton) findViewById(R.id.rb_weather);
        rbTrain = (RadioButton) findViewById(R.id.rb_train);
        rbBus = (RadioButton) findViewById(R.id.rb_bus);
        rbSmile = (RadioButton) findViewById(R.id.rb_smile);
    }


    //创建一个集合，用来存放viewpager的四个页面
    private List<BasePager> mPagerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_home);
        // initFragment();
        init();
        initView();
        initData();
        initListener();
        mPagerList.get(0).initData();//手动初始化首页数据
    }

    private void init() {

    }

    private void initView() {
        assignViews();
    }


    private void initData() {
        rgGroup.check(R.id.rb_weather);//默认勾选天气首页,每个RadioGroup中的RadioButton同时只能有一个被选中
        mPagerList = new ArrayList<>();
        mPagerList.add(new weatherPager(this));
        mPagerList.add(new trainPager(this));
        mPagerList.add(new busPager(this));
        mPagerList.add(new smilePager(this));
        //viewpager添加一个适配器
        vpContent.setAdapter(new contentAdapter());
    }

    private void initListener() {
        /**
         * 设置监听radioGroup的选择事件
         */
        rgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_weather:
                        vpContent.setCurrentItem(0, false);//false为去掉切换页面的动画
                        break;
                    case R.id.rb_train:
                        vpContent.setCurrentItem(1, false);//false为去掉切换页面的动画
                        break;
                    case R.id.rb_bus:
                        vpContent.setCurrentItem(2, false);//false为去掉切换页面的动画
                        break;
                    case R.id.rb_smile:
                        vpContent.setCurrentItem(3, false);//false为去掉切换页面的动画
                        break;

                    default:

                        break;
                }
            }
        });
        /**
         * 监听viewpager的页面变化，进行数据更新
         */
        vpContent.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mPagerList.get(position).initData();//获取当前被选中的页面，初始化该页面的数据
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    private class contentAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            if (mPagerList != null) {
                return mPagerList.size();
            }
            return 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager pager = mPagerList.get(position);
            container.addView(pager.mRootView);
            return pager.mRootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    /**
     * 初始化fragment，将fragment填充给布局文件
     */
    /*
    private void initFragment() {
        FragmentManager fm = getSupportFragmentManager();
        //开启一个事务
        FragmentTransaction transaction = fm.beginTransaction();
        ContentFragment contentFragment =new ContentFragment();
    }*/
}
